<?php

namespace App\Http\Controllers\Admin\Finance\Payable;

use App\Http\Controllers\Admin\Base\ExchangeRate;
use App\Http\Controllers\Controller;
use App\Http\Requests\airline\base\BanRequest;
use App\Models\airline\base\FlightModel as BaseFlightModel;
use App\Models\DictionaryModel;
use App\Models\finance\PayableTakeOrderDetailModel;
use App\Models\finance\PayableTakeOrderModel;
use App\Models\finance\PayableTmpModel;
use App\Models\OrderModel;
use App\Models\PayBillModel;
use App\Models\TakeOrderModel;
use App\Models\TakeOrderRecModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Bill extends Controller
{
    use BanRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $agent_id = $request->input('agent_id', '');



        $query = PayBillModel::query()->with(['agent', 'currency']);


        if($agent_id){
            $query->where('agent_id', $agent_id);
        }

//        if($currency_id){
//            $query->where('currency_id', $currency_id);
//        }



        $total = $query->count('id');

        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {

            $res = [
                'id' => $item->id,
                'agent_id' => $item->agent_id,
                'agent_name' => $item->agent->name ?? '',
                'sn' => $item->take_order->sn ?? '',
            ];
            return $res;
        });


        return $this->success(['list' => $list, 'total' => $total]);
    }




}
